lib.use(['element', 'table', 'form','treetable','jquery'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var $ = layui.jquery;
    var treetable = layui.treetable;
    // 渲染表格
    var renderTable = function () {
        treetable.render({
            treeColIndex: 1,//树形图标显示在第几列
            treeSpid: 0,//最上级的父级id
            treeIdName: 'id',//id字段的名称
            treePidName: 'parentId',//pid字段的名称
            treeDefaultClose: true,//是否默认折叠
            treeLinkage: true,//父级展开时是否自动展开所有子级
            elem: '#classTable',
            url: common.util.createUrl('/base/class/list'),
            page: false,
            cols: [[
                 {type: 'numbers', title: '编号'}
                ,{ title: '分类编码', field: 'classCode'}
                ,{ title: '分类名称', field: 'className'}
                ,{ title: '上级名称',field: 'parentName'}
                ,{ title: '分类类型', field: 'classType' ,templet: function (row) { return common.dist.getValue("base.Class_ClassTypeEnum",row.classType);}}
                ,{ title: '分类排序', field: 'classSeq'},
                {
                    fixed: 'right', title: '操作', width: 200, templet: function (row) {
                        return '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="add">添加子分类</a>' +
                        '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改</a>' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    }
                }
            ]],
           /* done: function () {
                layer.closeAll('loading');
            }*/
        });
        /**
         * 监听行单击事件，进入编辑事件
         */
        table.on('rowDouble(classTableFilter)', function(obj){
            update(obj)
        });

        //监听单元格事件
        table.on('tool(classTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                location.href = 'classForm.html?id=' + obj.data.id;
            }
            if (obj.event === 'del') {
                del(obj);
            }
            if (obj.event === 'add'){
                add(obj);
            }
        });
    };
    renderTable();
    $(document).on('click','#add',function(){


        var config={
            title:"新增分类",
            content:'base/class/classForm.html?' //iframe的url，no代表不显示滚动条
        }
        common.util.openWindow(busCommon.baseWindowConfig(config))
    });
   /* $(document).on('click','#add',function(){
        var config = {
            title:"新增计量单位",
            content: 'base/uom/uomForm.html?pId='+pId //iframe的url，no代表不显示滚动条
        }
        common.util.openWindow(busCommon.baseWindowConfig(config))
    });*/

    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        var keyword = params.className
        var searchCount = 0;
        $('#classTable').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
            $(this).css('background-color', 'transparent');
            var text = $(this).text();
            if (keyword != '' && text.indexOf(keyword) >= 0) {
                $(this).css('background-color', 'rgba(250,230,160,0.5)');
                if (searchCount == 0) {
                    treetable.expandAll('#classTable');
                    $('html,body').stop(true);
                    $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
                }
                searchCount++;
            }
        });
        if (keyword == '') {
            layer.msg("请输入搜索内容", {icon: 5});
        } else if (searchCount == 0) {
            layer.msg("没有匹配结果", {icon: 5});
        }
    }

    /**
     * 添加
     * @param obj
     */
    function add(obj) {
        location.href = 'classForm.html?parentName=' + obj.data.className+'&parentId='+obj.data.id;
    }
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        var config = {
            title:"修改分类",
            content: 'base/class/classForm.html?id=' + postData[0], //iframe的url，no代表不显示滚动条
        }
        common.util.openWindow(busCommon.baseWindowConfig(config))
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        var postData = [];
        postData = [obj.data.id]
        layer.confirm('真的删除吗', function(index){
            console.log('Menu Del param is', postData);
            common.ajax.get('/base/class/delete/'+postData[0], {}, function (resp) {
                renderTable();
                layer.close(index);
            })
        });
    }
});
